package com.demo.socket;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class SocketClient
{
	public static void main(String[] args) throws UnknownHostException, IOException
	{
		System.out.println("参数顺序：IP 端口 [请求报文文件绝对路径 长度位数]");
		String ip="127.0.0.1";
		int port=30000;
//		String ip="10.100.32.13";
//		int port=8080;
//		              bkzrcxsq
//		17 晓欢已做,20，,23，,28
		String requestXmlPath="src/main/java/com/demo/xml/E082.xml";
		int length=8;	//长度位数

		if(args!=null&&args.length>=2)
		{
			ip=args[0];
			port=Integer.parseInt(args[1]);
		}
		if(args!=null&&args.length>=3)
		{
			requestXmlPath=args[2];
		}
		if(args!=null&&args.length>=4)
		{
			length=Integer.parseInt(args[3]);
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(requestXmlPath)));
		String line=null;
		StringBuffer requestXml=new StringBuffer();
		while((line=br.readLine())!=null)
		{
			if (line.contains("<seqNb>")) {
				LocalDateTime localDateTime = LocalDateTime.now();
				DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
				line = line.substring(0,line.indexOf("<seqNb>")) + "<seqNb>" + localDateTime.format(dateTimeFormatter) +"</seqNb>";
			}
			requestXml.append(line).append("\n");
		}
		br.close();
		int sendDatalength=requestXml.toString().getBytes("UTF-8").length;
		String lengthStr=packString(sendDatalength+"","N",length);
		requestXml.insert(0, lengthStr);
		System.out.println("请求报文-->"+requestXml);

		Socket s=new Socket(ip,port);
		OutputStream out = s.getOutputStream();
		InputStream in = s.getInputStream();
		out.write(requestXml.toString().getBytes("UTF-8"));
		out.flush();

		
		byte[] receLengthByte = new byte[length];
		in.read(receLengthByte, 0, length);
		int receDatalength = Integer.parseInt(new String(receLengthByte));
		byte[] receData = new byte[receDatalength];
		in.read(receData, 0, receDatalength);
		String receMsg = new String(receData, "UTF-8");
		receMsg=receMsg.replaceAll("><", ">\n<");
		System.out.println("响应报文-->"+receMsg);
		
		out.close();
		in.close();
		s.close();
		
		
	}

	/**
	 *
	 * @param value
	 *            字段值
	 * @param type
	 *            字段类型
	 * @param maxLength
	 *            最大长度
	 * @return
	 */
	private static String packString(String value, String type, int maxLength)
	{
		StringBuffer result = new StringBuffer();
		if (value == null)
		{
			value = "";
		}
		if ("N".equals(type)) // 数字栏
		{
			result.append(getString("0", maxLength - value.length())).append(value);
		}
		else if ("AN".equals(type)) // 字符数字栏
		{
			result.append(value).append(getString(" ", maxLength - value.length()));
		}
		else if ("A".equals(type)) // 字符栏
		{
			result.append(value).append(getString(" ", maxLength - value.length()));
		}

		return result.toString();
	}
	
	private static String getString(String value, int count)
	{
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < count; i++)
		{
			result.append(value);
		}
		return result.toString();
	}
}
